package com.theyeasy.dvmini.service.impl;


import java.util.List;

import me.chanjar.weixin.common.exception.WxErrorException;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;






import org.theyeasy.weixin.service.WxOpenService;

import tk.mybatis.mapper.entity.Example;

import com.google.common.base.Strings;
import com.theyeasy.dvmini.dao.SysWxauthorizeDao;
import com.theyeasy.dvmini.model.SysWxauthorize;
import com.theyeasy.dvmini.service.SysWxauthorizeService;

@Service
public class SysWxauthorizeServiceImpl implements SysWxauthorizeService {
	
	@Autowired
	private SysWxauthorizeDao sysWxauthorizeDao;
	@Autowired
	private WxOpenService openService;
	

	@Override
	public SysWxauthorize getAuthorizerMpByAppIdAndMerchantId(String appId,int merchantId) {
		return getAuthorizerOnlyOne();
		
	}
	
	@Override
	public SysWxauthorize getAuthorizerOnlyOne()
	{
		SysWxauthorize sysWxauthorize=new SysWxauthorize();
		sysWxauthorize.setDeleted(false);
		return sysWxauthorizeDao.selectOne(sysWxauthorize);
	}
	
	@Override
	public int insertAuthorizerMp(SysWxauthorize sysWxauthorize) {
		// TODO Auto-generated method stub
		int inx= sysWxauthorizeDao.insertSelective(sysWxauthorize);
		return inx;
	}

	@Override
	public int updateAuthorizerMp(SysWxauthorize sysWxauthorize) {
		// TODO Auto-generated method stub
		return sysWxauthorizeDao.updateByPrimaryKeySelective(sysWxauthorize);
	}

	@Override
	public List<SysWxauthorize> getSysWxauthorizeAll() {
		// TODO Auto-generated method stub
		return sysWxauthorizeDao.selectAll();
	}

	@Override
	public void initWxOpenMpService() {
		// TODO Auto-generated method stub
		List<SysWxauthorize> list=getSysWxauthorizeAll();
		
		if(list!=null&&list.size()>0){
			for(SysWxauthorize auth :list){
				try {
					openService.initWxMpService(auth.getWxappid(), auth.getRefreshtoken());
				} catch (WxErrorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			
		}
	}

	/* (非 Javadoc)
	 * Description:
	 * @see com.theyeasy.wykj.service.SysWxauthorizeService#getDropDown(java.lang.String)
	 */
	@Override
	public List<SysWxauthorize> getDropDown(String keys) {
		// TODO Auto-generated method stub
		Example example=new Example(SysWxauthorize.class);
		Example.Criteria criteria= example.createCriteria();
		if(!Strings.isNullOrEmpty(keys)){
			criteria.andLike("wxname", "%"+keys+"%");
		}
		example.orderBy(" id desc");
		RowBounds rowBounds=new RowBounds(0, 50);
		return 	sysWxauthorizeDao.selectByExampleAndRowBounds(example, rowBounds);
	}

	@Override
	public List<SysWxauthorize> getDropDown(Integer type, String keys) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SysWxauthorize getSysWxauthorizeById(int id) {
		// TODO Auto-generated method stub
		return null;
	}



	

}
